package zhenbang
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	import flash.geom.*;
	public class RoundPlayer extends Sprite
	{
		private var stageW:int;
		//return 1920;
		private var stageH:int;
		//return 1080;
		public function RoundPlayer(w:int,h:int)
		{
			stageW = w;
			stageH= h;
			if(stage){
				init(null);
			}else{
				addEventListener(Event.ADDED_TO_STAGE,init);
			}
		}
		private function init(e:Event):void
		{
			clearTimeout(setTimeoutId);
			setTimeoutId = setTimeout(startRotate,3000);

			//removeEventListener(Event.ADDED_TO_STAGE,init);
			if(e.target!=this || e.currentTarget!=this)
				return;
			x = stageW/2;
			y = stageH/2;
			name = ""+uint(-1);
			root.transform.perspectiveProjection.fieldOfView = 5;
			parent.rotationX = 10;
			play(photoIndex);
		}
		private function get maxPhotos():int
		{
			if(Datas.jsonObject)
			{
				if(Datas.jsonObject.RoundPlayer.numRows>0)
					return Datas.jsonObject.RoundPlayer.numPictures;
			}
			return 8;
		}
		public var pathArr:Array;
		public var photoIndex:int;


		public function get totalTime():uint
		{
			var i:uint = 3000; 
			i+=5000;
			i+=1000;
			i+=1500;
			return i;
		}

		public function play(i:int):void
		{
			photoIndex = i;
			SwfLoader.SwfLoad(pathArr[photoIndex],loaded);
		}
		private function loadNext(i:int):void
		{
			SwfLoader.SwfLoad(pathArr[i],loaded);
		}
		private function loaded(e:Event):void
		{
			if(e && Event.COMPLETE == e.type)
			{
				var bmp:Bitmap = e.target.content as Bitmap;
				if(bmp){
					var mc:Sprite = new Sprite();
					mc.name = ""+photoIndex;
					mc.addChild(bmp);
					//mc.z=0;
					bmp.x = -bmp.width/2;
					bmp.y = -bmp.height/2;
					//CONFIG::debugging
					{
						mc.width = stageW/8;
					}
					mc.scaleY = mc.scaleX;
					addChild(mc);
					showUp(mc);
				}
			}
		}
		private function showUp(mc:Sprite):void
		{

			var _numChildren:int = numChildren;
			var toAngle:int = _numChildren/maxPhotos*360;
			var r:int = stageW/3;
			var angle:Number = -toAngle/180*Math.PI;
			var toX:int = -r*Math.cos(angle);
			var toY:int = +r*Math.sin(angle);
			TweenLite.to(mc,1.0,{x:toX,z:toY,y:0,rotationY:-this.rotationY});

			sortItems();

			photoIndex++;
			if(photoIndex>=pathArr.length)
				photoIndex= 0;
			if(numChildren<maxPhotos){
				loadNext(photoIndex);
			}
		}

		private var setTimeoutId:uint;
		private function startRotate():void
		{
			clearTimeout(setTimeoutId);
			setTimeoutId = setTimeout(freeze,5000);

			deltaAngle=2;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function freeze():void
		{
			clearTimeout(setTimeoutId);
			setTimeoutId = setTimeout(out,1000);

			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		private function out():void
		{
			clearTimeout(setTimeoutId);
			setTimeoutId = setTimeout(stop,1500);

			TweenLite.to(this,1.0,{y:-stageH/2});
		}

		private function stop():void
		{
			stopAndRemove();

			var _parent:PhotoPlayer = parent as PhotoPlayer;
			if(_parent)
			{
				//_parent.replay();
				_parent.play3x5(photoIndex);
			}
		}

		public function stopAndRemove():void
		{
			clearTimeout(setTimeoutId);

			while(numChildren>0)
			{
				var mc:Sprite = getChildAt(0) as Sprite;
				removeObj(mc);
			}
		}

		private function removeObj(mc:Sprite):void
		{
			if(mc)
			{
				if(mc.numChildren>0)
					ViewSet.removes(mc);

				if(mc.parent)
					mc.parent.removeChild(mc);
			}
		}
		private function sortItems():void
		{
			var arr:Array = new Array;
			for(var i:int = 0; i < numChildren; i++)
			{
				var mc:DisplayObject = getChildAt(i);
				mc.rotationY = -rotationY;
				arr.push(mc);
			}
			if(arr.length<2)
				return;
			arr.sort(depthSort);
			for(i = 0; i < arr.length; i++)
			{
				addChildAt(arr[i], i);
			}
		}

		private function depthSort(objA:DisplayObject, objB:DisplayObject):int
		{
			var posA:Vector3D = objA.transform.matrix3D.position;
			posA = this.transform.matrix3D.deltaTransformVector(posA);
			var posB:Vector3D = objB.transform.matrix3D.position;
			posB = this.transform.matrix3D.deltaTransformVector(posB);
			return posB.z - posA.z;
		}


		private var deltaAngle:int = 0;
		private function onEnterFrame(event:Event):void
		{
			rotationY += deltaAngle;
			if(numChildren>1)
				sortItems();
		}
	}
}
